package com.amigo.online.provider.merchant.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.amigo.online.common.base.controller.BaseController;
import com.amigo.online.common.core.response.Response;
import com.amigo.online.common.core.security.IgnoreSecurity;
import com.amigo.online.common.param.dto.merchant.CancelCollectDto;
import com.amigo.online.common.param.dto.merchant.GoodsCommentDto;
import com.amigo.online.common.param.result.goods.HomeGoodsInfo;
import com.amigo.online.common.param.result.merchant.CollectGoodsInfo;
import com.amigo.online.common.param.result.merchant.GoodsCommentInfo;
import com.amigo.online.common.param.result.merchant.MerchantCommentInfo;
import com.amigo.online.common.param.result.merchant.MerchantConcernListInfo;
import com.amigo.online.common.param.result.merchant.MerchantHomeInfo;
import com.amigo.online.provider.merchant.model.entity.Category;
import com.amigo.online.provider.merchant.model.entity.MerchantAdv;
import com.amigo.online.provider.merchant.service.GoodsCollectService;
import com.amigo.online.provider.merchant.service.GoodsCommentService;
import com.amigo.online.provider.merchant.service.GoodsService;
import com.amigo.online.provider.merchant.service.MerchantService;

/**
 * <code>MerchantCustomerController</code>
 * <p>
 * : 商家（普通人视角）controller
 * </p>
 * <p>
 *
 * @author LuChen
 * @version 1.0
 * @company Amigo
 * @time 2018年12月15日 下午1:46:53
 * @lastUpdate 无
 * </p>
 */
@RestController
@RequestMapping(value = "/merCustomer", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class MerchantCustomerController extends BaseController {

    @Autowired
    private MerchantService merchantService;

    @Autowired
    private GoodsCollectService goodsCollectService;

    @Autowired
    private GoodsCommentService goodsCommentService;

    @Autowired
    private GoodsService goodsService;


    /**
     * 根据id关注商家
     *
     * @param id 商家id
     * @return
     */
    @GetMapping("/concernMer/{id}")
    public ResponseEntity<Response> concernMerchant(@PathVariable("id") Long id) {
        boolean re = merchantService.concernMerchantById(id);
        return success(re);
    }

    /**
     * 根据id取消关注商家
     *
     * @param id
     * @return
     */
    @DeleteMapping("/cancelConcernMer/{id}")
    public ResponseEntity<Response> cancelConcernMerchant(@PathVariable("id") Long id) {
        boolean re = merchantService.cancelConcernMerchantById(id);
        return success(re);
    }

    /**
     * 根据id收藏商品
     *
     * @param id
     * @return
     */
    @GetMapping("/collectGood/{id}")
    public ResponseEntity<Response> collectGoods(@PathVariable("id") Long id) {
        boolean re = goodsCollectService.collectGoodById(id);
        return success(re);
    }

    /**
     * 根据id取消收藏商品
     *
     * @param id
     * @return
     */
    @PostMapping("/cancelCollectGood")
    public ResponseEntity<Response> cancelCollectGoods(@RequestBody CancelCollectDto dto) {
        boolean re = goodsCollectService.CancelCollectGoodById(dto.getIds());
        return success(re);
    }

    /**
     * 根据id评论商品
     *
     * @param id
     * @return
     */
    @PostMapping("/commentGood")
    public ResponseEntity<Response> commentGoods(@RequestBody GoodsCommentDto goodsCommentDto) {
        boolean re = goodsCommentService.commentGoodById(goodsCommentDto);
        return success(re);
    }

    /**
     * 根据id删除某条评论
     *
     * @param id
     * @return
     */
    @DeleteMapping("/deleteComment/{id}")
    public ResponseEntity<Response> deleteCommentGoods(@PathVariable("id") Long id) {
        boolean re = goodsCommentService.deletecommentById(id);
        return success(re);
    }

    /**
     * 获得关注列表 其中包括统计数
     *
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/concernMerList/{page}/{size}")
    public ResponseEntity<Response> getConcernMerList(@PathVariable("page") Integer page,
                                                      @PathVariable("size") Integer size) {
        MerchantConcernListInfo re = merchantService.getConcernMerList(page, size);
        return success(re);
    }

    /**
     * 获得收藏列表
     *
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/collectMerList/{page}/{size}")
    public ResponseEntity<Response> getCollectMerList(@PathVariable("page") Integer page,
                                                      @PathVariable("size") Integer size) {
        List<CollectGoodsInfo> re = merchantService.getCollectMerList(page, size);
        return success(re);
    }

    /**
     * 获得评论列表信息
     *
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/commentMerList/{page}/{size}")
    public ResponseEntity<Response> getCommentMerList(@PathVariable("page") Integer page,
                                                      @PathVariable("size") Integer size) {
        List<MerchantCommentInfo> re = goodsCommentService.getCommentMerList(page, size);
        return success(re);
    }

    /**
     * 获得首页广告
     *
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/adv")
    @IgnoreSecurity
    public ResponseEntity<Response> getHomePageAdv() {
        return success(merchantService.getHomePageAdv());
    }

    /**
     * 获得首页大类
     *
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/getHomePageCategory")
    @IgnoreSecurity
    public ResponseEntity<Response> getHomePageCategory() {
        return success(merchantService.getHomePageCategory());
    }


    /**
     * 获得商户首页数据
     *
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/homePage")
    public ResponseEntity<Response> getHomePage() {
        // 获得首页分类
        List<Category> category = merchantService.getHomePageCategory();
        // 获得首页广告
        List<MerchantAdv> adv = merchantService.getHomePageAdv();
        // 获取首页热门商品
        List<HomeGoodsInfo> homeGoods = goodsService.findHotGoods(null, null, null, null, null, null);
        JSONObject rs = new JSONObject();
        rs.put("adv", adv);
        rs.put("category", category);
        rs.put("homeGoods", homeGoods);
        return success(rs);
    }

    @GetMapping("/v2/homePage")
    public ResponseEntity<Response> getV2HomePage(@RequestHeader("Accept-Language") String name) {
        // 获得首页分类
        List<Category> category = merchantService.getHomePageCategory();
        // 获得首页广告
        List<MerchantAdv> adv = merchantService.getHomePageAdv();
        // 获取首页热门商家
        List<MerchantHomeInfo> homeMerchant = merchantService.findHotMerchant(name, null, null);
//		List<HomeGoodsInfo> homeGoods = goodsService.findHotGoods(null, null, null, null, null, null);
        JSONObject rs = new JSONObject();
        rs.put("adv", adv);
        rs.put("homeMerchant", homeMerchant);
        rs.put("category", category);
        return success(rs);
    }


    /**
     * 按照评论类型获得某商品评论列表
     *
     * @param id   商品id
     * @param type 评论类型 0 全部 1好评 2差评
     * @param page 页码
     * @param size 每页数量
     * @return
     */
    @GetMapping("/commentListByGoodId/{id}/{type}/{status}/{page}/{size}")
    public ResponseEntity<Response> getCommentGoodsList(@PathVariable("id") Integer id,
                                                        @PathVariable("type") Integer type, @PathVariable("status") Integer status,
                                                        @PathVariable("page") Integer page, @PathVariable("size") Integer size) {
        GoodsCommentInfo re = goodsCommentService.getCommentGoodsList(id, type, status, page, size);
        return success(re);
    }

    /**
     * 商户首页
     *
     * @param merchantId 商户id
     * @param page       页码
     * @param size       每页数量
     * @param type       查询类别 ,1热门 2推荐 3好评率
     * @return
     */
    @GetMapping("/merchantHome")
    public ResponseEntity<Response> getMerchantHome(@RequestParam("merchantId") Long merchantId,
                                                    @RequestParam("page") Integer page, @RequestParam("size") Integer size, HttpServletRequest request) {
        return success(merchantService.getMerchantHome(merchantId, page, size, request.getHeader("Accept-Language")));
    }


}
